package struts.form;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;

public class StrutsXML {
	public StrutsXML(){
		
	}
	public static Map<String,XmlBean> strutsXml(String xmlpath) throws Exception{
		SAXBuilder builder = new SAXBuilder();
		File file = new File(xmlpath);
		Document doc = builder.build(file);
		Element root = doc.getRootElement();
		Map<String,XmlBean> rMap = new HashMap<String,XmlBean>();
		Element actionroot = root.getChild("action-mapping");
		List<Element> actionList = actionroot.getChildren();
		
		
		for(Element e : actionList){
			XmlBean action = new XmlBean();
			
			String name = e.getAttributeValue("name");
			action.setBeanName(name);
			Element actionform = root.getChild("formbeans");
			List<Element> formList = actionform.getChildren();
			for(Element ex : formList){
				if(name.equals(ex.getAttributeValue("name"))){
					String formclass = ex.getAttributeValue("class");
					
					action.setFormClass(formclass);
					break;
				}
				
			}
			String path = e.getAttributeValue("path");
			action.setPath(path);
			String type = e.getAttributeValue("type");
			action.setActionClass(type);
			
			Map<String,String> map = new HashMap<String,String>();
			List<Element> forwardList = e.getChildren();
			for(Element e1: forwardList){
				String fname = e1.getAttributeValue("name");
				String fvalue = e1.getAttributeValue("value");
				map.put(fname, fvalue);
			}
			action.setActionForward(map);
			rMap.put(path, action);
		}
		return rMap;
		
	}
}
